<?php

App::uses('AppController', 'Controller');

/**
 * Paralelos Controller
 *
 * @property Paralelo $Paralelo
 */
class ParalelosController extends AppController
{

    public $uses = array('Paralelo', 'Nivele', 'Gestione');
    public $layout = 'colegio';

    public function beforeFilter()
    {
        parent::beforeFilter();
        //$this->Auth->allow('*');
    }

    public function index()
    {
        $gestion = date("Y");
        $paralelos = $this->Paralelo->find('all', array(
            'recursive' => 0,
            'conditions'=>array('Paralelo.gestion'=>$gestion)
        ));
        //debug($paralelos);
        $this->set(compact('paralelos'));
    }

    function insertar()
    {
        if (!empty($this->request->data))
        {
          
            $idNivel = $this->request->data['Paralelo']['nivele_id'];
            
           /* $nivel = $this->Nivele->find('first', 
                array('conditions'=>array('Nivele.id'=>$nivelId))
            );*/
             $nivel = $this->Nivele->find('first', array(
             'conditions'=>array('Nivele.id'=>$idNivel), 
             'recursive'=>-1
             ));
           
           
            $this->request->data['Paralelo']['curso'] = $nivel['Nivele']['nombre'];
            
            if ($this->Paralelo->save($this->request->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente!!!', 'msgbueno');
                $this->redirect(array('action' => 'index'));
                //debug($this->data);
            } else
            {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        $gestion =date('Y');
        $dpara = $this->Nivele->find('list', array(
                    'fields' => 'Nivele.nombre', 
                    'conditions'=>array('Nivele.gestion'=>$gestion)
                    ));
        $this->set(compact('dpara'));

        $dgest = $this->Gestione->find('list', array('fields' => array('Gestione.nombre','Gestione.nombre')));
        $this->set(compact('dgest'));
    }

    function editar($id = null)
    {
        $this->Paralelo->id = $id;
        if (!$id)
        {
            $this->Session->setFlash('No Existe el tipo de Paralelo');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data))
        {
            $this->data = $this->Paralelo->read();
        } else
        {
            $nivelId= $this->request->data['Paralelo']['nivele_id'];
            
            $nivel = $this->Nivele->find('first', array('conditions'=>array('Nivele.id'=>$nivelId)));
            $this->request->data['Paralelo']['curso']= $nivel['Nivele']['nombre'];
            //debug($this->request->data);exit;
            if ($this->Paralelo->save($this->request->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente el Paralelo');
                $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash('Error al guardar el Paralelo');
            }
        }
        $dpara = $this->Nivele->find('list', array('fields' => 'Nivele.nombre'));
        $this->set(compact('dpara'));

        $dgest = $this->Gestione->find('list', array('fields' => array('Gestione.nombre','Gestione.nombre')));
        $this->set(compact('dgest'));
    }

    function eliminar($id = null, $idNivel=null)
    {
        $this->Paralelo->id = $id;
        $this->data = $this->Paralelo->read();
        if (!$id)
        {
            $this->Session->setFlash('No existe el Paralelo a Eliminar');
            $this->redirect(array('action' => 'index'));
        } else
        {
            if ($this->Paralelo->delete($id))
            {
                $this->Session->setFlash('Paralelo eliminado!!!', 'msgbueno');
                $this->redirect(array('action' => 'index', $idNivel));
            } else
            {
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }

    public function listado($idNivel = null)
    {
        $paralelosNivel = $this->Paralelo->find('all', array(
            'recursive' => -1,
            'conditions' => array('Paralelo.nivele_id' => $idNivel)
         ));
        $nivel = $this->Nivele->find('first', array(
            'recursive'=>-1,
            'conditions'=>array('id'=>$idNivel)
        ));
        
        $this->set(compact('paralelosNivel', 'idNivel', 'nivel'));
    }
    
    

}
